﻿//import React from 'react'

export class ValidatorBase {
    constructor(message) {
        this.message = message
    }

    validate({value, children}) {
        return true
    }
}

class RequiredValidator extends ValidatorBase {
    validate({value, children}) {
        if (!value) {
            return this.message || '必须填写'
        }
        return null
    }
}

class EmailValidator extends ValidatorBase {
    validate({value, children}) {
        if (!value || !/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test(value)) {
            return this.message || '邮箱格式不正确'
        }
        return null
    }
}

export const required = (message) => {
    return new RequiredValidator(message)
}

export const email = (message) => {
    return new EmailValidator(message)
}


